package net.wofly.sale.report.schedule;

import net.wofly.common.util.DateUtil;
import net.wofly.sale.report.service.ICardExchangeDailyReportService;
import net.wofly.sale.report.service.ISaleDailyReportService;
import net.wofly.sale.report.service.ISaleStockDailyReportService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Calendar;

@Component
public class ReportSchedule {
    
    private static final Logger logger = LoggerFactory.getLogger(ReportSchedule.class);
    
    @Autowired
    ISaleDailyReportService iSaleDailyReportService;
    
    @Autowired
    ISaleStockDailyReportService iSaleStockDailyReportService;
    
    @Autowired
    ICardExchangeDailyReportService iCardExchangeDailyReportService;
    
    @Scheduled(cron = "${report.dailyReportCron}")
    public void orderTimeoutRefresh() {
        try {
            logger.info(">>> 开始 生成日报（report） 定时任务");
            Calendar cal = Calendar.getInstance();
            cal.add(Calendar.DATE, -1);
            Long startTime = DateUtil.processStartTime(cal.getTime()).getTime();
            logger.info(">>>统计日期（前一日）【" + DateUtil.getDateToString(cal.getTime(),
                    "yyyy-MM-dd") + "】的 startTime:【" + startTime + "】");
            
            logger.info(">>> 1开始 生成日报汇总");
            iSaleDailyReportService.generateSaleDailyReport(startTime);
            logger.info(">>> 1结束 生成日报汇总");
            
            logger.info(">>> 2开始 生成卡兑换日报");
            iCardExchangeDailyReportService.generateCardExchangeDailyReport(startTime);
            logger.info(">>> 2结束 生成卡兑换日报");
            
            logger.info(">>> 3开始 生成 销售库存汇总日报");
            iSaleStockDailyReportService.generateSaleStockDailyReport(startTime);
            logger.info(">>> 3结束 生成 销售库存汇总日报");
            
        } catch (Exception e) {
            logger.error(">>> 生成日报（report）定时任务 执行失败：", e);
        }
        logger.info(">>> 结束执行 生成日报（report） 定时任务");
    }
    
}
